/**
 * <p>Title: ServiceZMCE71.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 张小飞
 * @version 1.0
 * <pre>Histroy:
 *          2012-1-5 下午04:28:27  张小飞  Create
 *</pre>
 */
package com.baosight.sczx.zm.ce.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiColumn;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.ce.Equipment;
import com.baosight.sczx.common.zm.cInterface.cs.InitialData;
import com.baosight.sczx.zm.ce.domain.ZMCEFaultReason;

/**
 * <p>Title: ServiceZMCE71.java</p>
 * <p>Description: 故障原因列表</p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 张小飞
 * @version 1.0
 * <pre>Histroy:
 *          2012-1-5 下午04:28:27  张小飞  Create
 *</pre>
 */
public class ServiceZMCE71 extends ServiceEPBase {

	/**
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCE71</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-1-5下午04:44:32 张小飞
	 * </p>
	 *
	*/
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);
		return outInfo;
	}
	
	/**
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCE71</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-1-5下午04:44:32 张小飞
	 * </p>
	 *
	*/
	@Override
	public EiInfo query(EiInfo inInfo) {
		StaticTools.setQueryParameters(inInfo, "fdToencode", inInfo.getString("fdToencode"), true);
		EiInfo outInfo = super.query(inInfo, "ZMCEFaultReason.query", new ZMCEFaultReason());
		outInfo.addBlock(Equipment.getStandardEquipmentBlock("cmes.ce.equipment", null));
		outInfo.addBlock(initBlock("cmes.ce.solutionMethod", "popwindowResult"));
		return outInfo;
	}

	/**
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCE71</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-1-5下午04:44:32 张小飞
	 * </p>
	 *
	*/
	@Override
	public EiInfo insert(EiInfo inInfo) {
		//新增一条故障原因记录
		String fdCode = this.generateCode(inInfo.getBlock("result").getCellStr(0, "fdToencode"));
		if("$".equals(fdCode)){
			inInfo.setStatus(-1);
			inInfo.setMsg("编号的最后四位尾码已经超出了编号规则所规定的四位，请调整已有的数据！");
			return inInfo;
		}
		inInfo.getBlock("result").setCell(0, "fdCode",fdCode);
		String faultReasonKey = TablePk.getInstance().getNextValue("T_CE_FAULTREASON");
		inInfo.getBlock("result").setCell(0, "fdKeyId", faultReasonKey);
		EiInfo outInfo = super.insert(inInfo, "ZMCEFaultReason.insert", "result");
		
		//新增一条原因解决对应记录
		for(int i=0; i < inInfo.getBlock("popwindowResult").getRowCount(); i++){
			inInfo.getBlock("popwindowResult").setCell(i, "fdKeyId", TablePk.getInstance().getNextValue("T_CE_REASONMETHOD"));
			inInfo.getBlock("popwindowResult").setCell(i, "fdFaultreasonId", faultReasonKey);
		}
		super.insert(inInfo, "ZMCEReasonMethod.insert", "popwindowResult");
		
		return outInfo;
	}

	/**
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCE71</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-1-5下午04:44:32 张小飞
	 * </p>
	 *
	*/
	@Override
	public EiInfo update(EiInfo inInfo) {
		String fdFaultreasonId = inInfo.getBlock("result").getCellStr(0, "fdKeyId");
		
		//修改故障原因记录
		EiInfo outInfo = super.update(inInfo, "ZMCEFaultReason.update", "result");
		
		//修改原因解决对应记录:先删除，再新增
		inInfo.addBlock("mappingResult");
		inInfo.getBlock("mappingResult").setCell(0, "fdFaultreasonId", fdFaultreasonId);
		super.delete(inInfo, "ZMCEReasonMethod.deleteMapping", "mappingResult");
		
		for(int i=0; i < inInfo.getBlock("popwindowResult").getRowCount(); i++){
			inInfo.getBlock("popwindowResult").setCell(i, "fdKeyId", TablePk.getInstance().getNextValue("T_CE_REASONMETHOD"));
			inInfo.getBlock("popwindowResult").setCell(i, "fdFaultreasonId", fdFaultreasonId);
		}
		super.insert(inInfo, "ZMCEReasonMethod.insert", "popwindowResult");
		
		return outInfo;
	}
	
	/**
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCE71</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-1-5下午04:44:32 张小飞
	 * </p>
	 *
	*/
	@Override
	public EiInfo delete(EiInfo inInfo) {
		//删除原因解决对应记录
		for(int i=0; i < inInfo.getBlock("result").getRowCount(); i++){
			String fdFaultreasonId = inInfo.getBlock("result").getCellStr(i, "fdKeyId");
			inInfo.addBlock("mappingResult");
			inInfo.getBlock("mappingResult").setCell(i, "fdFaultreasonId", fdFaultreasonId);
		}
		super.delete(inInfo, "ZMCEReasonMethod.deleteMapping", "mappingResult");
		
		//删除故障原因记录
		EiInfo _temp = super.delete(inInfo, "ZMCEFaultReason.delete", "result");
		EiInfo outInfo = this.query(inInfo);
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
	}

	/**
	 * 
	 * <p>Function: generateCode</p>
	 * <p>Description: 自动生成故障现象编号</p>
	 * <p>return value:String</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-1-9上午10:22:43 张小飞
	 * </p>
	 *
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	private String generateCode(String toencode){
		String code = "";
		List<Map> _list = dao.query("ZMCEFaultReason.queryLatestCode", null);
		Object fdCode = _list.get(0).get("fdCode");
		if(fdCode == null){
			code = "YY" + toencode + "0001";
		}else{
			String added = (Integer.parseInt(fdCode.toString())+1) + "";
			if(added.length() == 1){
				code = "YY" + toencode + "000" + added;
			}else if(added.length() == 2){
				code = "YY" + toencode + "00" + added;
			}else if(added.length() == 3){
				code = "YY" + toencode + "0" + added;
			}else if(added.length() == 4){
				code = "YY" + toencode + added;
			}else{
				code = "$";//此处设置的“$”为一个固定标识,切勿随意修改
			}
		}
		return code;
	}
	
	/**
	 * 
	 * <p>Function: initBlock</p>
	 * <p>Description: 将初始数据“解决方法”，加工成弹出页面中的列表页面绑定的block</p>
	 * <p>return value:EiBlock</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-1-13上午09:51:14 张小飞
	 * </p>
	 *
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	private EiBlock initBlock(String oldName, String newName) {
		EiBlock oldBlock = InitialData.getInstance().getMoreEiBlockByNo(oldName);
		
		EiBlock newBlock = new EiBlock(newName);
		
		EiColumn eiColumn = null;
		eiColumn = new EiColumn("fdSystemdataId");
		eiColumn.setFieldLength(32);	
		eiColumn.setDescName("解决办法ID");
		newBlock.addMeta(eiColumn);
		
		eiColumn = new EiColumn("fdMethodName");
		eiColumn.setFieldLength(30);
		eiColumn.setDescName("解决办法名称");
		newBlock.addMeta(eiColumn);
		
		eiColumn = new EiColumn("fdMethodDescription");
		eiColumn.setFieldLength(250);
		eiColumn.setDescName("解决方法描述");
		newBlock.addMeta(eiColumn);

		List<Map> rows = oldBlock.getRows();
		Map<String, String> rowMap = null;
		
		if(rows != null && !rows.isEmpty()){
			for(Map row : rows){
				String solution = (String) row.get("value");
				String name = (String) row.get("display");
				String description = (String) row.get("remark");
				
				rowMap = new HashMap();
				rowMap.put("fdSystemdataId", solution);
				rowMap.put("fdMethodName", name);
				rowMap.put("fdMethodDescription", description);
				newBlock.addRow(rowMap);
			}
		}
		
		return newBlock;
	}
	
}
